package crowMessenger;


import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class AddAccountFrame extends JDialog{
	private static final long serialVersionUID = -5510669334591196780L;
	private JTextField userInput = new JTextField(20);
	private JPasswordField passInput = new JPasswordField(20);
	protected MainPanel con;
	
	/* constructor */
	public AddAccountFrame(MainPanel con){
		this.con = con;
		userInput.setToolTipText("(E.g. crowmessenger@gmail.com)"); 	//tooltips
		setTitle("Add a new account");
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setLocation(600, 150);
		setResizable(false);
		setModal(true);
		getContentPane().add(addAccountPanel());
		pack();
		setVisible(true);
	}
	
	/* main panel for this frame */
	private JPanel addAccountPanel() {
		JPanel panel = new JPanel(new GridLayout(2, 1));
		panel.setPreferredSize(new Dimension(500, 300));
		panel.add(upPanel());
		panel.add(downPanel());
		return panel;
	}
	
	/* Contains the two other panels containing username and password */
	private JPanel upPanel(){
		JPanel panel = new JPanel(new GridLayout(2,1));
		panel.add(userPanel());
		panel.add(passPanel());
		return panel;
	}
	
	/* contains the buttons panel */
	private JPanel downPanel(){
		JPanel panel = new JPanel(new GridLayout(2, 4));
		//start of dummy panels
		for(int i = 0; i < 6; i++){
			JPanel tempPanel = new JPanel();
			panel.add(tempPanel);
		}
		//end of dummy panels
		panel.add(cancelPanel());
		panel.add(okPanel());
		return panel;
	}
	
	/* contains the ok button */
	private JPanel okPanel(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("OK");
		btn.setPreferredSize(new Dimension(100,30));
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(userInput.getText().length() >= 16 && userInput.getText().endsWith("@gmail.com") && passInput.getPassword().length >= 8){
					Account acc = new Account(userInput.getText(), new String(passInput.getPassword()),con);
					Accounts.addNew(acc);
					repaint();
					revalidate();
					setVisible(false);
				}
			}
		});
		panel.add(btn);
		return panel;
	}
	
	/* contains the cancel button */
	private JPanel cancelPanel(){
		JPanel panel = new JPanel();
		JButton btn = new JButton("Cancel");
		btn.setPreferredSize(new Dimension(100,30));
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		panel.add(btn);
		return panel;
	}
	
	/* contains the username label and input field */
	private JPanel userPanel(){
		JPanel panel = new JPanel(new GridBagLayout());
		JLabel label = new JLabel("Username     ");
		panel.add(label);
		panel.add(userInput);
		return panel;
	}
	
	/* contains the password label and input field */
	private JPanel passPanel(){
		JPanel panel = new JPanel(new GridBagLayout());
		JLabel label = new JLabel("Password     ");
		panel.add(label);
		panel.add(passInput);
		return panel;
	}
	
}
